<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once dirname(__FILE__)."/Sql.class.php";
require_once dirname(__FILE__)."/Craft.class.php";
require_once dirname(__FILE__)."/TradeskillContainer.class.php";
require_once dirname(__FILE__)."/ObjectContainer.class.php";

class CraftContainer
{
	static private $instance;
	
	private $crafts = null;
	
	private $sql;
	private $tradeskill_container;
	
	private function __construct()
	{
		$this->sql = Sql::GetInstance();
		$this->tradeskill_container = TradeskillContainer::GetInstance();
	}
	
	static function GetInstance()
	{
		if ( empty(self::$instance) )
		{
			self::$instance = new CraftContainer();
		}
		
		return self::$instance;
	}
	
	function GetCraftsByTradeskillId($id, $s=null)
	{
		if ($this->crafts == null)
		{
			$this->crafts = array();
			$items = array();
			$spells = array();
			$tradeskill = $this->tradeskill_container->GetTradeskillById($id);
			
			$this->sql->Query("
				SELECT
					`cp_professions`.`profession_custom_id`,
					`cp_professions`.`ProfessionName`,
					`cp_professions`.`CategoryName`,
					`cp_professions`.`SpellId`,
					`cp_professions`.`ItemId` as `PItemId`,
					GROUP_CONCAT(DISTINCT `cp_professions`.`Locale`) AS `Locales`,
					GROUP_CONCAT(DISTINCT `cp_professions`.`CraftName` SEPARATOR '_\$_') AS `CraftNames`,
					GROUP_CONCAT(DISTINCT `cp_professions`.`CharacterName`) AS `Characters`,
					GROUP_CONCAT(DISTINCT CONCAT(`cp_reagents`.`ItemId`, ':', `cp_reagents`.`ItemQuantity`) ) `Reagents`
				FROM
					`cp_professions`
				LEFT JOIN
					`cp_reagents`
				ON
					`cp_reagents`.`reagent_profession_custom_id`=`cp_professions`.`profession_custom_id`
				WHERE
					`ProfessionName` IN ('".implode("','", $tradeskill->GetLocales())."')
				GROUP BY
					`cp_professions`.`profession_custom_id`,
					`cp_reagents`.`reagent_profession_custom_id`
				ORDER BY
					`cp_professions`.`SpellId` DESC,
					`cp_professions`.`ItemId` DESC
			");
			
			while ( $craft = $this->sql->IterateResults() )
			{
				$this->crafts[$craft['profession_custom_id']] = new Craft($craft);
				
				foreach( $this->crafts[$craft['profession_custom_id']]->GetReagents() as $reagent_id => $reagent_data )
				{
					if ( !in_array($reagent_id, $items) )
					{
						array_push($items, $reagent_id);
					}
				}
				
				if ( $craft['PItemId'] != 0 and !in_array($craft['PItemId'], $items) )
				{
					array_push($items, $craft['PItemId']);
				}
				elseif ( $craft['SpellId'] != 0 and !in_array($craft['SpellId'], $spells) )
				{
					array_push($spells, $craft['SpellId']);
				}
			}
			 
			$object_container = ObjectContainer::GetInstance();
			
			$object_container->GetObjects($items, $spells);
			
			if ( $s != null )
			{
				$s = html_entity_decode($s, ENT_QUOTES);
				
				foreach ($this->crafts as $key_craft => $craft)
				{
					$found = false;
					
					foreach($craft->GetNamesAll() as $name)
					{
						if ( stripos($name, $s) !== FALSE )
						{
							$found = true;
							break;
						}
					}
					
					if ($found == false)
					{
						if ($craft->IsItem())
						{
							$tooltips = $object_container->GetItem($craft->GetItemId())->GetTooltipAll();
						}
						else
						{
							$tooltips = $object_container->GetSpell($craft->GetSpellId())->GetTooltipAll();
						}
						
						foreach($tooltips as $tooltip)
						{
							if ( stripos($tooltip, $s) !== FALSE )
							{
								$found = true;
								break;
							}
						}
					}
					
					if ($found == false)
					{
						unset($this->crafts[$key_craft]);
					}
				}
			}
		}
		
		return $this->crafts;
	}
}

?>